package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/7 16:44
 */
public class M0205 {

    // https://leetcode-cn.com/problems/sum-lists-lcci/
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // 个->十->百   +   个->十->百   =    个->十->百->千
        return recur(l1, l2, 0);
    }

    public static ListNode recur(ListNode l1, ListNode l2, int carry) {
        if (l1 == null && l2 == null) {
            return carry == 0 ? null : new ListNode(carry);
        }
        ListNode cur;
        int sum;
        sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
        cur = new ListNode(sum % 10);
        cur.next = recur(l1 == null ? null : l1.next, l2 == null ? null : l2.next, sum > 9? 1: 0);
        return cur;
    }

}
